package com.example.intelligenttransportation;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText s_name, s_password;
    private CheckBox s_mm, s_dl;
    private Button s_btn1, s_btn2;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        jump();
        checkBoxAction();
        s_mm.setOnClickListener(this);
        s_dl.setOnClickListener(this);
        s_btn1.setOnClickListener(this);
        s_btn2.setOnClickListener(this);
    }

    private void checkBoxAction() {
        sp=getSharedPreferences("sign1", Context.MODE_PRIVATE);
        String smm = sp.getString("s_mm","");
        String sdl=sp.getString("s_dl","");
        if(smm.equals("true")){
            s_mm.setChecked(true);
            sp=getSharedPreferences("sign1", Context.MODE_PRIVATE);
            String name = sp.getString("name","");
            String password=sp.getString("password","");
            s_name.setText(name);
            s_password.setText(password);
        }
        if(sdl.equals("true")){
            s_mm.setChecked(true);
            s_dl.setChecked(true);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    login();
                }
            }).start();

        }
    }

    private void initView() {
        s_name=findViewById(R.id.s_name);
        s_password=findViewById(R.id.s_password);
        s_mm=findViewById(R.id.s_mm);
        s_dl=findViewById(R.id.s_dl);
        s_btn1=findViewById(R.id.s_btn1);
        s_btn2=findViewById(R.id.s_btn2);
    }

    private void jump(){
        sp=getSharedPreferences("sign", Context.MODE_PRIVATE);
        String sign = sp.getString("sign","");
        if(!sign.equals("sign")){
            startActivity(new Intent(MainActivity.this,LogoActivity.class));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.s_btn1:
                login();
                break;
            case R.id.s_btn2:
                reg();
                break;
            case R.id.s_dl:
                s_mm.setChecked(true);
                break;
            case R.id.s_mm:
                s_dl.setChecked(false);
                break;
            default:
                break;
        }
    }

    private void reg() {
        Toast.makeText(MainActivity.this,"开发中",Toast.LENGTH_SHORT).show();
    }

    private void login() {
        sp=getSharedPreferences("sign1", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("s_mm", String.valueOf(s_mm.isChecked()));
        edit.putString("s_dl", String.valueOf(s_dl.isChecked()));
        if(s_mm.isChecked()&&s_name.getText().toString().trim().equals("user1")&&
                s_password.getText().toString().trim().equals("123456")){
            edit.putString("name", s_name.getText().toString().trim());
            edit.putString("password", s_password.getText().toString().trim());
        }
        edit.apply();
        if(s_name.getText().toString().trim().equals("user1")&&s_password.getText().toString().trim().equals("123456")) {
            startActivity(new Intent(MainActivity.this, SuccessActivity.class));
        }else {Toast.makeText(MainActivity.this,"程序错误",Toast.LENGTH_SHORT).show();}
    }
}
